<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org"
      xmlns:shiro="http://www.pollix.at/thymeleaf/shiro">
<head th:include="include :: header"></head>
<body class="hold-transition skin-yellow sidebar-mini">
<!--导入模态框-->
<div class="modal fade" data-backdrop="static" data-keyboard="false" id="modal-import-plan">
    <div class="modal-dialog">
        <div class="modal-content">
            <div class="modal-header">
                <button type="button" class="close" data-dismiss="modal" aria-label="Close">
                    <span aria-hidden="true">&times;</span></button>
                <h4 class="modal-title">Excel信息导入</h4>
            </div>
            <div class="modal-body">
                <form id="importForm">
                    <div class="form-group">
                        <label>模版</label>
                        <a class="out" th:href="@{/file/发货计划模板.xlsx}">点击下载</a>
                    </div>
                    <div class="form-group">
                        <label>选择Excel文件并上传</label>
                        <input id="import-file" class="btn" type="file" name="file">
                        <p class="help-block">请按模板格式进行导入，信息与模板信息对应。</p>
                    </div>
                </form>
            </div>
            <div class="modal-footer">
                <button id="btn-import" type="button" class="btn btn-success">
                    <span class="Bold">确定</span>
                </button>
                <button type="button" class="btn btn-default" data-dismiss="modal">取消</button>
            </div>
        </div>
    </div>
</div>
<section class="content">

    <input type="hidden" class="form-control" name="name" id="name">
    <button id="btn_add"  class="btn btn-info pull-right"
            onclick="saveMaintain()">保存
    </button>
    <a  href="javascript:void(0);" class="btn btn-success pull-right" style="margin-right: 10px"
        onclick="js.modal.open('modal-import-plan')">
        <span class="Bold">导入</span>
    </a>
    <form id="partForm" class="form-horizontal" role="form">
        <input type="hidden" id="id" name="id" th:value="${instShipmentsPlanSheet.id}">
        <div class="col-xs-11 col-xs-offset-1">
            <table class="t1 t3" id="hoursTable">
                <thead>
                <tr>
                    <td colspan="9">
                        <img th:src="@{/img/logo3.jpg}" width="64px" height="40px" style="margin-top: -20px">
                        <div style="display: inline-block;margin-top: 5px;margin-left: 20px">
                            <span style="letter-spacing: 24px;display: block;line-height: 20px">&nbsp;辽宁忠旺机械设备制造有限公司</span>
                            <span style="display: block;font-size: 12px;line-height: 20px">Liaoning Zhongwang Machinery Equipment Manufacturing Co., Ltd</span>
                        </div>
                    </td>
                    <td colspan="2" style="width: 12%;font-size: 20px;">发货计划</td>
                    <td></td>
                </tr>
                <tr>
                    <td style="width: 5%">生产令号</td>
                    <td colspan="1" style="width: 12%;font-size: 14px;">
                        <input id="sheetForm-proCode" class="form-control" name="proCode"
                               th:value="${instShipmentsPlanSheet.proCode}" readonly>
                    </td>
                    <td style="width: 9%">项目名称</td>
                    <td colspan="3" style="width: 18%">
                        <input id="sheetForm-proName" type="text" class="form-control" name="proName"
                               th:value="${instShipmentsPlanSheet.proName}" readonly>
                    </td>
                    <td style="width: 5%">需方</td>
                    <td colspan="4" style="width: 5%">
                        <input id="sheetForm-customer" class="form-control" name="customer"
                               th:value="${instShipmentsPlanSheet.customer}" readonly>
                    </td>
                    <td></td>
                </tr>
                <tr>
                    <td style="width: 5%">负责人</td>
                    <td colspan="2" style="width: 15%;font-size: 14px;">
                        <input id="sheetForm-leader" class="form-control" name="leader"
                               th:value="${instShipmentsPlanSheet.leader}">
                    </td>
                    <td style="width: 5%">接收人</td>
                    <td colspan="2" style="width: 12%">
                        <input id="sheetForm-relMan" type="text" class="form-control" name="relMan"
                               th:value="${instShipmentsPlanSheet.relMan}">
                    </td>
                    <td style="width: 15%">计划发货时间</td>
                    <td colspan="4" style="width: 8%">
                        <input id="planTime" type="text" class="form-control datepicker" name="planTime"
                               th:value="${#dates.format(instShipmentsPlanSheet.planTime, 'yyyy-MM-dd')}"
                               readonly="readonly" style="width: 100%;"/>
                    </td>
                    <td></td>
                </tr>
                <tr>
                    <td style="width: 10%">收货地址</td>
                    <td colspan="10" style="width: 80%;font-size: 14px;">
                        <input id="sheetForm-address" class="form-control" name="address"
                               th:value="${instShipmentsPlanSheet.address}">
                    </td>
                    <td></td>
                </tr>
                </thead>
                <input id="sheetId" type="hidden" name="sheetId" class="form-control">
                <thead>
                <tr>
                    <td style="width: 4%">序号</td>
                    <td colspan="2">型号</td>
                    <td colspan="2">零件名称</td>
                    <td style="width: 6%">数量</td>
                    <td style="width: 6%">单位</td>
                    <td colspan="2">发货状态</td>
                    <td colspan="2">备注</td>
                    <td></td>
                </tr>
                </thead>
                <tbody id="out">
                <tr orderNum="orderNum" th:each="purchPlanDetail :${purchDetail}">
                    <td beforeTdOrdernum="beforeTdOrdernum" th:text="${purchPlanDetailStat.count}"></td>
                    <td colspan="2">
                        <input type="text" class="form-control" name="drawNo"
                               th:value="${purchPlanDetail.drawNo}" autocomplete="off">
                    </td>
                    <td colspan="2">
                        <input type="text" class="form-control" name="drawName"
                               th:value="${purchPlanDetail.drawName}" autocomplete="off">
                    </td>
                    <td>
                        <input type="text" class="form-control" name="drawNum"
                               th:value="${purchPlanDetail.drawNum}" autocomplete="off">
                    </td>
                    <td>
                        <input type="text" class="form-control" name="unit" th:value="${purchPlanDetail.unit}"
                               autocomplete="off">
                    </td>
                    <td colspan="2">
                        <input type="text" class="form-control" name="location"
                               th:value="${purchPlanDetail.location}" autocomplete="off">
                    </td>
                    <td colspan="2">
                        <input type="text" class="form-control" name="remark"
                               th:value="${purchPlanDetail.remark}" autocomplete="off">
                    </td>
                    <td>
                        <span class="out-add" title="添加"><i class="fa fa-plus text-info"></i> </span>
                        <span class="out-del" title="删除"><i class="fa fa-remove text-red"></i> </span>
                    </td>
                </tr>
            </table>
        </div>
    </form>
</section>
<div th:include="include :: footer"></div>
<script th:inline="javascript">
    let table, table1, table2;
    // /** 项目编号下拉框 */
    // js.select2({
    //     id: 'sheetForm-proCode',
    //     placeholder: '',
    //     ajax: {
    //
    //         url: function () {
    //             return ctx + 'create/shipments/project/list'
    //         },
    //         processResults: function (data, page) {
    //
    //             let array = data;
    //             for (let i = 0; i < data.length; i++) {
    //                 array[i]["proId"] = array[i]['proId'];
    //                 array[i]["id"] = array[i]['proCode'];
    //                 array[i]["text"] = array[i]['proCode'];
    //
    //             }
    //             return {results: array};
    //         },
    //     }
    // });
    /**添加明细选择按钮*/
    $("#btn_product").click(function () {
        let sheetId = $("#query2-id").val();
        //查询制作件
        if (sheetId == '') {
            js.modal.warning("请选择要添加明细的单据！！");
            return;
        }
        // window.open(ctx + "create/shipments/addProduct?sheetId=" + sheetId);
        location.href = ctx + "create/shipments/addProduct?sheetId=" + sheetId;

    });
    /** 项目编号下拉框选中 */
    $("#sheetForm-proCode").on("select2:select", function () {
        $("#sheetForm-proName").val($(this).select2("data")[0].proName);
        $("#sheetForm-customer").val($(this).select2("data")[0].customer);
        $("#proId").val($(this).select2("data")[0].proId);
        $("#sheetForm-proCode").val($(this).select2("data")[0].proCode);
    });

    /**保存模态框*/
    function saveMaintain() {
        let res = js.vd({
            id: 'partForm',
            notEmpty: ['proCode', 'planTime', 'leader', 'relMan', 'address']
        })
        if (!res) {
            return;
        }
        var url = ctx + "create/shipments/edit/PlanSheet";
        js.validSubmit({
            formId: "partForm",
            url: url,
            data: new FormData($("#partForm")[0]),
            success: function (result) {
                if (result.type === web_status.SUCCESS) {
                    js.modal.success(result.msg);
                    location.href = ctx + "create/shipmentsEdit";
                    // js.table.refresh(table);
                    // js.modal.hide("myModal");
                } else {
                    js.modal.warning(result.msg);
                }
            }
        });

    }

    $(function () {
        /** 加载列表 */
        table1 = js.table.init({
            id: 'product-table',
            url: ctx + "create/shipmentsEdit/prodectDetail",
            showExport: false,
            queryForm: "queryForm2",
            pageSize: 5,
            uniqueId: 'id',
            columns: [
                {

                    title: '序号', field: 'id', width: '50',
                    formatter: function (value, row, index) {
                        return index + 1;
                    }
                },
                {title: "类型", field: 'special'},
                {title: "部分", field: 'part'},
                {title: "零件名称", field: 'drawName'},
                {title: "图号", field: 'drawNo'},
                {title: "发货数量", field: 'drawNum'},
                {title: "图纸类别", field: 'drawType'},
                {title: "存放地点", field: 'location'},
                {
                    title: "操作",
                    formatter: function (value, row) {
                        let actions = [];
                        actions.push('<a class="btn btn-danger btn-xs" onclick="delProduct(' + row.id + ')" > 发货退库</a> ');
                        return actions.join('');
                    }
                }
            ]
        });
        /** 添加一行 */
        $("#partForm").on('click', '.out-add', function () {
            $(this).parents("tr").after(`
                    <tr orderNum="orderNum">
                        <td beforeTdOrdernum="beforeTdOrdernum">1</td>
                            <td colspan="2">
                                <input type="text" class="form-control" name="drawNo" autocomplete="off">
                            </td>
                            <td colspan="2">
                                <input type="text" class="form-control" name="drawName" autocomplete="off">
                            </td>
                            <td>
                                <input type="text"  class="form-control" name="drawNum" autocomplete="off">
                            </td>
                            <td>
                                <input type="text" class="form-control" name="unit" autocomplete="off">
                            </td>
                            <td colspan="2">
                                <input type="text" class="form-control" name="location" autocomplete="off">
                            </td>
                            <td colspan="2">
                                <input type="text" class="form-control" name="remark" autocomplete="off">
                            </td>
                        <td>
                            <span class="out-add" title="添加"><i class="fa fa-plus text-info"></i> </span>
                            <span class="out-del" title="删除"><i class="fa fa-remove text-red"></i> </span>
                        </td>
                    </tr>
            `);
            beforeCompuTrNoH();
        });
        /** 删除行 */
        $('#partForm').on('click', '.out-del', function () {
            $(this).parents("tr").remove();
            if ($("table tbody tr").length === 0) {
                $("#out").append(`
                                <tr orderNum="orderNum">
                                    <td beforeTdOrdernum="beforeTdOrdernum">1</td>
                                     <td colspan="2">
                                <input type="text" class="form-control" name="drawNo" autocomplete="off">
                            </td>
                                <td colspan="2">
                                <input type="text" class="form-control" name="drawName" autocomplete="off">
                            </td>
                            <td>
                                <input type="text"  class="form-control" name="drawNum" autocomplete="off">
                            </td>
                            <td>
                                <input type="text" class="form-control" name="unit" autocomplete="off">
                            </td>
                            <td colspan="2">
                                <input type="text" class="form-control" name="location" autocomplete="off">
                            </td>
                            <td colspan="2">
                                <input type="text" class="form-control" name="remark" autocomplete="off">
                            </td>
                                    <td>
                                        <span class="out-add" title="添加"><i class="fa fa-plus text-info"></i> </span>
                                        <span class="out-del" title="删除"><i class="fa fa-remove text-red"></i> </span>
                                    </td>
                                </tr>
                            `)
            }
            beforeCompuTrNoH();
        });
    });


    /** tr表格变更后的序号的计算*/
    function beforeCompuTrNoH() {
        $("table  tr[orderNum='orderNum']").each(function (i, e) {
            $(this).children("td[beforeTdOrdernum='beforeTdOrdernum']").text(i + 1);
        })
    }

    /** 导入 */
    $('#btn-import').click(function () {

        let files = $("#import-file")[0].files;
        // let files = e.target.files;
        let fileReader = new FileReader();
        fileReader.onload = function (ev) {
            try {
                let data = ev.target.result,
                    workbook = XLSX.read(data, {type: 'binary'}), // 以二进制流方式读取得到整份excel表格对象
                    plans = []; // 存储获取到的数据
                // 遍历每张表读取
                for (let sheet in workbook.Sheets) {
                    if (workbook.Sheets.hasOwnProperty(sheet)) {
                        plans = plans.concat(XLSX.utils.sheet_to_json(workbook.Sheets[sheet]));
                        break; // 只取第一张表
                    }
                }
                if (plans.length === 0) {
                    js.modal.alert("无法读取文件内容");
                } else {
                    $("#out").empty();
                    plans.forEach((value, index) => {
                        let s = value['发货状态'] || '';
                        let a = value['型号'] || '';
                        let b = value['零件名称'] || '';
                        let c = value['数量'] || '';
                        let d = value['单位'] || '';
                        let r = value['备注'] || '';
                        $("#out").append(`
                                 <tr orderNum="orderNum">
                                    <td beforeTdOrdernum="beforeTdOrdernum">1</td>
                                     <td colspan="2">
                                        <input type="text" class="form-control" name="drawNo" value="${a}" autocomplete="off">
                                    </td>
                                    <td colspan="2">
                                        <input type="text" class="form-control" name="drawName" value="${b}" autocomplete="off">
                                    </td>
                                    <td>
                                        <input type="text" class="form-control" name="drawNum" value="${c}" autocomplete="off">
                                    </td>
                                    <td>
                                        <input type="text" class="form-control" name="unit" value="${d}"  autocomplete="off">
                                    </td>
                                    <td colspan="2">
                                        <input type="text" class="form-control" name="location" value="${s}"  autocomplete="off">
                                    </td>
                                    <td colspan="2">
                                        <input type="text" class="form-control" name="remark" value="${r}"  autocomplete="off">
                                    </td>
                                    <td>
                                        <span class="out-add" title="添加"><i class="fa fa-plus text-info"></i> </span>
                                        <span class="out-del" title="删除"><i class="fa fa-remove text-red"></i> </span>
                                    </td>
                                </tr>
                            `);
                    });
                    beforeCompuTrNoH();
                    js.modal.hide("modal-import-plan");
                }
            } catch (e) {
                js.modal.alert("无法读取文件或文件类型有错误");
            }
        };
        // 以二进制方式打开文件
        fileReader.readAsBinaryString(files[0]);
    });
    /** 保存计划单 */
    $("#btn_saveDetail").click(function () {
        let $this = $(this);
        let res = js.vd({
            id: 'sheetForm',
            notEmpty: ['drawName', 'drawNum', 'unit', 'location']
        })
        if (!res) {
            return;
        }
        $this.attr('disabled', true);
        js.submit({
            formId: "sheetForm",
            url: ctx + 'create/shipments/addPurchList',
            data: new FormData($("#sheetForm")[0]),
            success: function (result) {
                if (result.type === web_status.SUCCESS) {
                    js.modal.success(result.msg);
                    location.href = ctx + "create/shipmentsEdit";
                } else {
                    js.modal.warning(result.msg);
                }
            }
        });
        $this.removeAttr('disabled');
    });

    /** 删除制作件明细单 */
    function delProduct(id) {
        js.modal.confirm("确定要删除发货单吗？", function () {
            js.post({
                url: ctx + "create/shipments/delProduct",
                data: {id: id},
                success: function (result) {
                    if (result.type === web_status.SUCCESS) {
                        js.modal.success(result.msg);
                        js.table.refresh(table1);
                    } else {
                        js.modal.warning(result.msg);
                    }
                }
            })
        })
    }
</script>
</body>
</html>